﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Text.RegularExpressions;

namespace TheTVDB
{
    public class Banner
    {
        public enum BannerType
        {
            SeriesText, SeriesGraphical, SeriesBlank, Season, SeasonWide, Fanart
        }


        public int ID { get; set; }
        public string Path { get; set; }
        public BannerType Type { get; set; }
        public int FanartWidth { get; set; }
        public int FanartHeight { get; set; }
        public string Language { get; set; }
        public string VignettePath { get; set; }
        public string FanartThumbnailPath { get; set; }
        public int Season { get; set; }
        public BannerColor LightColor { get; set; }
        public BannerColor MidColor { get; set; }
        public BannerColor DarkColor { get; set; }
        public Banner()
        {

        }



        private static Regex resolution = new Regex(@"^(\d+)x(\d+)$");
        public static Banner FromXml(XmlNode mirrorNode)
        {
            Banner newMirror = new Banner();
            
            foreach (XmlElement child in mirrorNode.ChildNodes)
            {

                switch (child.Name)
                {
                    case "id":
                        newMirror.ID = Int32.Parse(child.InnerText);
                        break;
                    case "BannerPath":
                        newMirror.Path = child.InnerText;
                        break;
                    case "VignettePath":
                        newMirror.VignettePath = child.InnerText;
                        break;
                    case "ThumbnailPath":
                        newMirror.FanartThumbnailPath = child.InnerText;
                        break;
                    case "BannerType":
                        switch (child.InnerText)
                        {
                            case "series":
                                newMirror.Type = BannerType.SeriesBlank;
                                break;
                            case "season":
                                newMirror.Type = BannerType.Season;
                                break;
                            case "fanart":
                                newMirror.Type = BannerType.Fanart;
                                break;
                        }
                        break;
                    case "BannerType2":
                        Match m = resolution.Match(child.InnerText);
                        if (m.Success)
                        {
                            newMirror.Type = BannerType.Fanart;
                            newMirror.FanartWidth = Int32.Parse(m.Groups[1].Value);
                            newMirror.FanartHeight = Int32.Parse(m.Groups[2].Value);
                        }
                        else
                        {
                            switch (child.InnerText)
                            {
                                case "text":
                                    newMirror.Type = BannerType.SeriesText;
                                    break;
                                case "blank":
                                    newMirror.Type = BannerType.SeriesBlank;
                                    break;
                                case "graphical":
                                    newMirror.Type = BannerType.SeriesGraphical;
                                    break;
                                case "season":
                                    newMirror.Type = BannerType.Season;
                                    break;
                                case "seasonwide":
                                    newMirror.Type = BannerType.SeasonWide;
                                    break;
                            }
                        }
                        break;
                    case "Season":
                        int season;
                        if(Int32.TryParse(child.InnerText, out season))
                            newMirror.Season = season;
                        break;
                    case "Language":
                        newMirror.Language = child.InnerText;
                        break;
                    case "Colors":
                        string[] parts = child.InnerText.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
                        if (parts.Length != 3)
                            break;

                        newMirror.LightColor = BannerColor.FromString(parts[0]);
                        newMirror.MidColor = BannerColor.FromString(parts[1]);
                        newMirror.DarkColor = BannerColor.FromString(parts[2]);

                        break;
                    default:
                        break;
                }
            }


            return newMirror;
        }

        public struct BannerColor 
        { 
            public byte R; 
            public byte G; 
            public byte B;
            public BannerColor(byte r, byte g, byte b)
            {
                this.R = r;
                this.G = g;
                this.B = b;
            }
            public static BannerColor FromString(string s)
            {
                string[] parts = s.Split(new char[]{','}, StringSplitOptions.RemoveEmptyEntries);
                if (parts.Length != 3)
                    return new BannerColor();

                return new BannerColor(Byte.Parse(parts[0]), Byte.Parse(parts[1]), Byte.Parse(parts[2]));
            }
        }
    }
}
